package com.axend.aerosense.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.axend.aerosense.home.LogDetailActivity;
import com.axend.aerosense.home.ReportLogActivity;
import com.axend.aerosense.home.adapter.ReportAdapter;
import com.axend.aerosense.home.entity.ReportLogListBean;
import com.axend.aerosense.home.viewmodel.ReportLogViewModel;
import com.blankj.utilcode.util.ToastUtils;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.IOperationDetailResult;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.bean.EarlyWarningBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.home.R;
import com.axend.aerosense.home.databinding.HomeFragemntReportBinding;

import java.util.ArrayList;

public class ReportLogListFragment extends MvvmBaseFragment<HomeFragemntReportBinding, ReportLogViewModel>
        implements ILoadDataResult<ReportLogListBean> {
    private String roomId;
    private ReportAdapter adapter;
    private ArrayList<EarlyWarningBean> mList;
    public static final String ROOM_ID = "ROOM_ID";
    private boolean isNext = true;

    public static ReportLogListFragment getInstance(String roomId){
        ReportLogListFragment fragment = new ReportLogListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ROOM_ID, roomId);
        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    protected void initParameters() {
        super.initParameters();
        Bundle bundle = getArguments();
        if(null != bundle){
            roomId = bundle.getString(ROOM_ID);
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_fragemnt_report;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected ReportLogViewModel getViewModel() {
        BaseViewModelFactroy<ReportLogListBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        ReportLogViewModel tmp = new ViewModelProvider(this, myViewModelFactroy).get(ReportLogViewModel.class);
        tmp.setRoomId(roomId);
        return tmp;
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void onResume() {
        super.onResume();
        ((ITitle)getActivity()).setTitle(getString(R.string.home_warning_title));
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.REPORT_EVENT_CHANGE ,Integer.class)
                .observe(getViewLifecycleOwner(), bean-> {
                    // 信息变更时重新刷新
                    viewModel.onLoad();
                });
    }

    private void changeFragment(Fragment fragment){
        FragmentUtil.add(getActivity(), ((ReportLogActivity)getActivity()).getContentId(), fragment, this, true);
    }

    public void init(){
        adapter = new ReportAdapter();
        viewDataBinding.userList.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.userList.setAdapter(adapter);

        adapter.setOnItemClickListener((adapter, view, position) -> {
            EarlyWarningBean earlyWarningBean = (EarlyWarningBean) adapter.getData().get(position);
            Intent intent = new Intent(this.getActivity(), LogDetailActivity.class);
            intent.putExtra(Gobal.IntentParamType.EARLY_OBJECT, earlyWarningBean);
            this.getActivity().startActivity(intent);
        });

        setLoadSir(viewDataBinding.homeContentView);
        showLoading();
        viewModel.onLoad();

        viewDataBinding.homeRefreshLayout.setOnRefreshListener(refreshLayout -> {
            viewModel.onLoad();
        });
        viewDataBinding.homeRefreshLayout.setOnLoadMoreListener(listener -> {
            viewModel.getMoreData(viewModel.pageIndex + 1, new IOperationDetailResult<ReportLogListBean>() {
                @Override
                public void onSuccess(ReportLogListBean reportLogListBean) {
                    isNext = reportLogListBean.isNext();
                    viewModel.pageIndex ++;
                    adapter.getData().addAll(reportLogListBean.getEarlyWarningLogList());
                    adapter.notifyDataSetChanged();
                    viewDataBinding.homeRefreshLayout.finishLoadMore();
                    if(!isNext){
                        viewDataBinding.homeRefreshLayout.setNoMoreData(true);
                    }
                }

                @Override
                public void onFail(int errCode, String message) {
                    if(!TextUtils.isEmpty(message)){
                        ToastUtils.showShort(message);
                    }
                    viewDataBinding.homeRefreshLayout.finishLoadMore();
                }
            });
        });
    }

    @Override
    public void loadDataSuccess(ReportLogListBean alarmBean) {
        mList = alarmBean.getEarlyWarningLogList();
        isNext = alarmBean.isNext();
        if(null == mList || mList.size() == 0){
            showEmpty();
        }else{
            adapter.setList(mList);
        }
        viewDataBinding.homeRefreshLayout.finishRefresh();
        if(!isNext){
            viewDataBinding.homeRefreshLayout.setNoMoreData(true);
        }

    }

    @Override
    public void loadDataFail(String decs) {
        viewDataBinding.homeRefreshLayout.finishRefresh();
    }
}
